package com.liuzhongni.wahahanews.adapter;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.liuzhongni.wahahanews.R;
//import com.liuzhongni.wahahanews.headlineclass.MyMemoryCache;
import com.liuzhongni.wahahanews.headlineclass.ResponseEty;
import com.liuzhongni.wahahanews.headlineclass.ResponseEtySecond;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by dllo on 16/1/8.
 */
public class ListViewAdapterOfHeadline extends BaseAdapter {
    private Context context;
    private ResponseEty ety;


//    private RequestQueue mQueue;
//    private ImageLoader imageLoader;

    public ListViewAdapterOfHeadline(Context context, ResponseEty ety) {
        this.context = context;
        this.ety = ety;
//        mQueue = Volley.newRequestQueue(context);
//        imageLoader = new ImageLoader(mQueue, );
    }

    @Override
    public int getCount() {
        return ety.getT1348647909107().size() - 1;
    }


    @Override
    public Object getItem(int position) {
        return ety.getT1348647909107().get(position);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return ety.getT1348647909107().get(position).getImgType();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Type0ViewHolderHeadline type0Holder;
        Type1ViewHolderHeadline type1Holder;
        Type2ViewHolderHeadline type2Holder;
        final Type3ViewHolderHeadline type3Holder;
        View converView0 = null;
        View converView1 = null;
        View converView2 = null;
        View converView3 = null;
        Log.d("ListViewAdapterOfHeadli", "ety.getT1348647909107().get(position).getImgType():" + ety.getT1348647909107().get(position).getImgType());
        if (converView0 == null || converView1 == null || converView2 == null || converView3 == null) {
            switch (ety.getT1348647909107().get(position).getImgType()) {
                case 0:
                    if (ety.getT1348647909107().get(position).getAds() != null) {
                        Log.d("ListViewAdapterOfHeadli", "哇哈哈1");
                        converView3 = LayoutInflater.from(context).inflate(R.layout.listview_headview, null);
                        type3Holder = new Type3ViewHolderHeadline(converView3);
                        type3Holder.adapter = new ViewPagerAdapterOfNewHeadline(context, ety.getT1348647909107().get(0).getAds(), ety);
                        type3Holder.viewPager.setAdapter(type3Holder.adapter);
                        type3Holder.viewPager.setCurrentItem(Integer.MAX_VALUE / 2 / 4 * 4);
                        type3Holder.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                Log.d("ListViewAdapterOfHeadli", "position:" + position);
                                int index = position % 4;
                                for (int i = 0; i < 4; i++) {
                                    if (i == index) {
                                        type3Holder.imageViews[i].setImageResource(R.mipmap.iconfont_dian_selected);
                                    } else {
                                        type3Holder.imageViews[i].setImageResource(R.mipmap.iconfont_dian);
                                    }
                                }
                                type3Holder.textView.setText(ety.getT1348647909107().get(0).getAds().get(index).getTitle());
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });
                        converView3.setTag(type3Holder);
                        Log.d("ListViewAdapterOfHeadli", "ety.getT1348647909107().get(0).getAds().size():" + ety.getT1348647909107().get(0).getAds().size());
                        type3Holder.textView.setText(ety.getT1348647909107().get(0).getAds().get(position).getTitle());
                        return converView3;

                    } else if (ety.getT1348647909107().get(position).getImgextra() == null) {
                        Log.d("ListViewAdapterOfHeadli", "哇哈哈2");
                        converView0 = LayoutInflater.from(context).inflate(R.layout.new_headline_type0, null);
                        type0Holder = new Type0ViewHolderHeadline(converView0);
                        converView0.setTag(type0Holder);
                        VolleySingleton.getOurInstance().imgJson(type0Holder.imageViewType0, ety.getT1348647909107().get(position).getImgsrc());
                        type0Holder.textViewTitleType0.setText(ety.getT1348647909107().get(position).getTitle());
                        type0Holder.textViewDigestType0.setText(ety.getT1348647909107().get(position).getDigest());
                        if (ety.getT1348647909107().get(position).getSkipType() != null) {
                            if (ety.getT1348647909107().get(position).getSkipType().equals("special")) {
                                type0Holder.textViewReplyCountType0.setVisibility(View.INVISIBLE);
                                type0Holder.textViewzhuanti.setVisibility(View.VISIBLE);
                            } else {
                                type0Holder.textViewReplyCountType0.setText(ety.getT1348647909107().get(position).getReplyCount() + "跟帖");
                                type0Holder.textViewzhuanti.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            type0Holder.textViewReplyCountType0.setText(ety.getT1348647909107().get(position).getReplyCount() + "跟帖");
                            type0Holder.textViewzhuanti.setVisibility(View.INVISIBLE);
                        }

                        return converView0;
                    } else if (ety.getT1348647909107().get(position).getImgextra() != null) {

                        converView1 = LayoutInflater.from(context).inflate(R.layout.new_headline_type1, null);
                        type1Holder = new Type1ViewHolderHeadline(converView1);
                        converView1.setTag(type1Holder);
                        Log.d("ListViewAdapterOfHeadli", "哇哈哈3");
                        type1Holder.textViewTitleType1.setText(ety.getT1348647909107().get(position).getTitle());
                        type1Holder.textViewReplyCountType1.setText(ety.getT1348647909107().get(position).getReplyCount() + "");
                        VolleySingleton.getOurInstance().imgJson(type1Holder.imageViewFirstType1, ety.getT1348647909107().get(position).getImgsrc());
                        VolleySingleton.getOurInstance().imgJson(type1Holder.imageViewSecondType1, ety.getT1348647909107().get(position).getImgextra().get(0).getImgsrc());
                        VolleySingleton.getOurInstance().imgJson(type1Holder.imageViewThirdType1, ety.getT1348647909107().get(position).getImgextra().get(1).getImgsrc());
                        return converView1;
                    }

                case 1:
                    Log.d("ListViewAdapterOfHeadli", "哇哈哈4");
                    converView2 = LayoutInflater.from(context).inflate(R.layout.new_headline_type2, null);
                    type2Holder = new Type2ViewHolderHeadline(converView2);
                    converView2.setTag(type2Holder);
                    type2Holder.textViewTitleType2.setText(ety.getT1348647909107().get(position).getTitle());
                    VolleySingleton.getOurInstance().imgJson(type2Holder.imageViewType2, ety.getT1348647909107().get(position).getImgsrc());
                    type2Holder.textViewDigestType2.setText(ety.getT1348647909107().get(position).getDigest());
                    type2Holder.textViewReplyCountType2.setText(ety.getT1348647909107().get(position).getReplyCount() + "");
                    return converView2;
            }
        } else {

            switch (ety.getT1348647909107().get(position).getImgType()) {
                case 0:
                    if (ety.getT1348647909107().get(position).getAds() != null) {

                        type3Holder = (Type3ViewHolderHeadline) converView3.getTag();
                        type3Holder.adapter = new ViewPagerAdapterOfNewHeadline(context, ety.getT1348647909107().get(0).getAds(), ety);
                        type3Holder.viewPager.setAdapter(type3Holder.adapter);


                    } else if (ety.getT1348647909107().get(position).getImgextra() == null) {

                        type0Holder = (Type0ViewHolderHeadline) converView0.getTag();
                        VolleySingleton.getOurInstance().imgJson(type0Holder.imageViewType0, ety.getT1348647909107().get(position).getImgsrc());
                        type0Holder.textViewTitleType0.setText(ety.getT1348647909107().get(position).getTitle());
                        type0Holder.textViewDigestType0.setText(ety.getT1348647909107().get(position).getDigest());
                        if (ety.getT1348647909107().get(position).getSkipType() != null) {
                            if (ety.getT1348647909107().get(position).getSkipType().equals("special")) {
                                type0Holder.textViewReplyCountType0.setVisibility(View.INVISIBLE);
                                type0Holder.textViewzhuanti.setVisibility(View.VISIBLE);
                            } else {
                                type0Holder.textViewReplyCountType0.setText(ety.getT1348647909107().get(position).getReplyCount() + "跟帖");
                                type0Holder.textViewzhuanti.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            type0Holder.textViewReplyCountType0.setText(ety.getT1348647909107().get(position).getReplyCount() + "跟帖");
                            type0Holder.textViewzhuanti.setVisibility(View.INVISIBLE);
                        }
                        break;
                    } else if (ety.getT1348647909107().get(position).getImgextra() != null) {
                        type1Holder = (Type1ViewHolderHeadline) converView1.getTag();
                        type1Holder.textViewTitleType1.setText(ety.getT1348647909107().get(position).getTitle());
                        type1Holder.textViewReplyCountType1.setText(ety.getT1348647909107().get(position).getReplyCount() + "");
                        VolleySingleton.getOurInstance().imgJson(type1Holder.imageViewFirstType1, ety.getT1348647909107().get(position).getImgsrc());
                        VolleySingleton.getOurInstance().imgJson(type1Holder.imageViewSecondType1, ety.getT1348647909107().get(position).getImgextra().get(0).getImgsrc());
                        VolleySingleton.getOurInstance().imgJson(type1Holder.imageViewThirdType1, ety.getT1348647909107().get(position).getImgextra().get(1).getImgsrc());
                        break;
                    }
                case 1:

                    type2Holder = (Type2ViewHolderHeadline) converView2.getTag();
                    type2Holder.textViewTitleType2.setText(ety.getT1348647909107().get(position).getTitle());
                    VolleySingleton.getOurInstance().imgJson(type2Holder.imageViewType2, ety.getT1348647909107().get(position).getImgsrc());
                    type2Holder.textViewDigestType2.setText(ety.getT1348647909107().get(position).getDigest());
                    type2Holder.textViewReplyCountType2.setText(ety.getT1348647909107().get(position).getReplyCount() + "");
                    break;
            }
        }
        return null;
    }

    public class Type0ViewHolderHeadline {
        private NetworkImageView imageViewType0;
        private TextView textViewTitleType0;
        private TextView textViewDigestType0;
        private TextView textViewReplyCountType0;
        private TextView textViewzhuanti;

        public Type0ViewHolderHeadline(View view) {
            imageViewType0 = (NetworkImageView) view.findViewById(R.id.type0_iv);
            textViewTitleType0 = (TextView) view.findViewById(R.id.type0_tv_title);
            textViewDigestType0 = (TextView) view.findViewById(R.id.type0_tv_digest);
            textViewReplyCountType0 = (TextView) view.findViewById(R.id.type0_tv_replyCount);
            textViewzhuanti = (TextView) view.findViewById(R.id.type0_tv_zhuanti);
        }
    }

    public class Type1ViewHolderHeadline {
        private TextView textViewTitleType1;
        private TextView textViewReplyCountType1;
        private NetworkImageView imageViewFirstType1;
        private NetworkImageView imageViewSecondType1;
        private NetworkImageView imageViewThirdType1;

        public Type1ViewHolderHeadline(View view) {
            textViewTitleType1 = (TextView) view.findViewById(R.id.type1_tv_title);
            textViewReplyCountType1 = (TextView) view.findViewById(R.id.type1_tv_replyCount);
            imageViewFirstType1 = (NetworkImageView) view.findViewById(R.id.type1_iv_first);
            imageViewSecondType1 = (NetworkImageView) view.findViewById(R.id.type1_iv_second);
            imageViewThirdType1 = (NetworkImageView) view.findViewById(R.id.type1_iv_third);
        }
    }

    public class Type2ViewHolderHeadline {
        private NetworkImageView imageViewType2;
        private TextView textViewTitleType2;
        private TextView textViewDigestType2;
        private TextView textViewReplyCountType2;

        public Type2ViewHolderHeadline(View view) {
            imageViewType2 = (NetworkImageView) view.findViewById(R.id.type2_iv);
            textViewTitleType2 = (TextView) view.findViewById(R.id.type2_tv_title);
            textViewDigestType2 = (TextView) view.findViewById(R.id.type2_tv_digest);
            textViewReplyCountType2 = (TextView) view.findViewById(R.id.type2_tv_replyCount);
        }
    }

    public class Type3ViewHolderHeadline {
        private ViewPager viewPager;
        private TextView textView;
        private ViewPagerAdapterOfNewHeadline adapter;
        private int[] dotID;
        private ImageView[] imageViews;

        public Type3ViewHolderHeadline(View view) {
            dotID = new int[]{R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4};
            imageViews = new ImageView[4];
            for (int i = 0; i < 4; i++) {
                imageViews[i] = (ImageView) view.findViewById(dotID[i]);
            }
            viewPager = (ViewPager) view.findViewById(R.id.new_column_headline_vp);
            textView = (TextView) view.findViewById(R.id.headline_viewpager_tv);
        }
    }

    public void upData() {
        notifyDataSetChanged();
    }

    public void addItem(ArrayList<ResponseEtySecond> responseEtySecond) {
        ety.getT1348647909107().addAll(responseEtySecond);
        notifyDataSetChanged();
    }


}
